package me.monster.lab.terimal;

import java.util.ArrayList;
import java.util.List;

/**
 * @description
 * @author: Created jiangjiwei in 2020/10/22 09:59
 */
public class QueryResult {
    private int code;
    private List<QueryResultData> data;

    public int getCode() {
        return code;
    }

    public List<QueryResultData> getData() {
        return data == null ? data = new ArrayList<>() : data;
    }

    public boolean success() {
        return code == 0;
    }

    public String getSelectText() {
        StringBuilder sb = new StringBuilder();
        final List<QueryResultData> list = getData();
        for (int i = 0; i < list.size(); i++) {
            final QueryResultData queryResultData = list.get(i);
            sb.append((i + 1)).append(" ")
                    .append(queryResultData.getFilename()).append("\n")
                    .append("  ").append(queryResultData.getFilepath()).append("\n");
        }
        return sb.toString();
    }

    public String getPathByPosition(int index) {
        final List<QueryResultData> queryResultData = getData();
        if (index < 0 || index >= queryResultData.size()) {
            return "";
        }
        return queryResultData.get(index).getFilepath();
    }

    static class QueryResultData {
        public String filename;
        public String filepath;

        public String getFilename() {
            return filename == null ? "" : filename;
        }

        public void setFilename(String filename) {
            this.filename = filename;
        }

        public String getFilepath() {
            return filepath == null ? "" : filepath;
        }

        public void setFilepath(String filepath) {
            this.filepath = filepath;
        }
    }
}
